package main

import (
	"fmt"
	"gitee.com/virtual-moon/vload"
)

// Item 展示不同类型的示例
type Item struct {
	// 整型示例
	UserID   int64  // 用户ID
	UserName string // 用户名称

	// 字符串示例
	Code     string // 商品编码
	CodeDesc string // 商品描述

	// 浮点型示例
	Score     float64 // 评分
	ScoreDesc string  // 评分描述
}

func main() {
	// 模拟数据
	items := []Item{
		{UserID: 1001, Code: "A001", Score: 4.5},
		{UserID: 1002, Code: "B001", Score: 3.8},
		{UserID: 1003, Code: "C001", Score: 4.9},
	}

	// 1. 整型映射示例
	userMap := map[int64]string{
		1001: "张三",
		1002: "李四",
		1003: "王五",
	}
	err := vload.LoadQuick(&items, "UserID", "UserName", func(ids []int64) (map[int64]string, error) {
		result := make(map[int64]string)
		for _, id := range ids {
			if name, ok := userMap[id]; ok {
				result[id] = name
			}
		}
		return result, nil
	})
	if err != nil {
		fmt.Printf("加载用户名称失败: %v\n", err)
		return
	}

	// 2. 字符串映射示例
	codeMap := map[string]string{
		"A001": "高级商品",
		"B001": "中级商品",
		"C001": "初级商品",
	}
	err = vload.LoadQuick(&items, "Code", "CodeDesc", func(codes []string) (map[string]string, error) {
		result := make(map[string]string)
		for _, code := range codes {
			if desc, ok := codeMap[code]; ok {
				result[code] = desc
			}
		}
		return result, nil
	})
	if err != nil {
		fmt.Printf("加载商品描述失败: %v\n", err)
		return
	}

	// 3. 浮点型映射示例
	getScoreDesc := func(scores []float64) (map[float64]string, error) {
		result := make(map[float64]string)
		for _, score := range scores {
			switch {
			case score >= 4.5:
				result[score] = "优秀"
			case score >= 4.0:
				result[score] = "良好"
			default:
				result[score] = "一般"
			}
		}
		return result, nil
	}
	err = vload.LoadQuick(&items, "Score", "ScoreDesc", getScoreDesc)
	if err != nil {
		fmt.Printf("加载评分描述失败: %v\n", err)
		return
	}

	// 打印结果
	for _, item := range items {
		fmt.Printf("用户: %s, 商品: %s, 评分: %s\n",
			item.UserName, item.CodeDesc, item.ScoreDesc)
	}
}

/*
支持的可比较类型包括但不限于：
- 所有整数类型：int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64
- 浮点数类型：float32, float64
- 字符串类型：string
- 布尔类型：bool
- 复数类型：complex64, complex128
*/
